package com.ld.rightsSystem.controller;


import com.ld.rightsSystem.Enum.ResponseEnum;
import com.ld.rightsSystem.dto.TRoleDTO;
import com.ld.rightsSystem.exception.DBDataNotFoundException;
import com.ld.rightsSystem.pojo.TRole;
import com.ld.rightsSystem.service.inf.TRoleService;
import com.ld.rightsSystem.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author lhb
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/admin/role")
public class TRoleController {
    @Autowired
    private TRoleService tRoleService;

    /**
     * 添加角色
     * @param tRoleDTO
     * @return
     */
    @PostMapping("/add")
    public R addTRole(@RequestBody TRoleDTO tRoleDTO){
        return tRoleService.addTRole(tRoleDTO);
    }


    /**
     * 修改角色
     * @param tRoleDTO
     * @return
     */
    @PutMapping("/update")
    public R editTRole(@RequestBody TRoleDTO tRoleDTO) throws DBDataNotFoundException {
        return tRoleService.editTRole(tRoleDTO);
    }

    /**
     * 分页查询角色列表
     * @param pageNum
     * @param pageSize
     * @param checkRoleName
     * @param checkRoleCode
     * @return
     */
    @GetMapping("/query")
    public R getTRoleList(@RequestParam(value = "pageNum",required = false)Integer pageNum,
                          @RequestParam(value = "pageSize",required = false)Integer pageSize,
                          @RequestParam(value = "checkRName",required = false) String checkRoleName,
                          @RequestParam(value = "checkRCode",required = false) String checkRoleCode,
                          @RequestParam(value = "roleId",required = false) Long roleId) throws DBDataNotFoundException {
        if(roleId != null){
            return tRoleService.findRoleInfoByRoleId(roleId);
        }else if(pageNum != null && pageSize != null){
            return tRoleService.getTRoleList(pageNum,pageSize);
        }else if(checkRoleName != null || checkRoleCode != null){
            return tRoleService.checkFieldAvailable(checkRoleName,checkRoleCode);
        }else {
            return new R().error(ResponseEnum.PARAMS_BINDING_ERROR);
        }
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteTRoleById(@RequestParam("roleId") Long id) throws DBDataNotFoundException {
        return tRoleService.deleteTRoleById(id);
    }

}

